package org.rendao.service
{
	import com.googlecode.flexxb.FlexXBEngine;
	
	import org.rendao.entity.SearchResult;
	import org.rendao.entity.SecondIndex;
	import org.rendao.service.base.ServiceUtils;

	public class SecondIndexService
	{
		private static var _instance:SecondIndexService;
		
		public static function get instance():SecondIndexService {
			if (!_instance) {
				_instance=new SecondIndexService(new SingletonForcer());
			}
			return _instance;
		}
		
		public function SecondIndexService(sf:SingletonForcer) {
		}
		
		private static const GET_BY_FIRST_INDEX_URL:String = 
			"service/second_index/get_by_first_index/{first_index_id}";
		
		public function getByFirstIndex(firstIndexId:String, callback:Function):void {
			var onGet:Function = function(result:Object):void {
				var resultXml:XML = result as XML;
				var searchResult:SearchResult = FlexXBEngine.instance.deserialize(resultXml, SearchResult);
				callback(searchResult.records);
			};
			var url:String = GET_BY_FIRST_INDEX_URL.replace("{first_index_id}", firstIndexId);
			ServiceUtils.callService(url, onGet);
		}
		
		private static const SAVE_URL:String = "service/second_index/save";
		public function save(secondIndex:SecondIndex, callback:Function):void {
			var onSave:Function = function(data:Object):void {
				var resultXml:XML = data as XML;
				var result:SecondIndex = FlexXBEngine.instance.deserialize(resultXml, SecondIndex);
				callback(result);
			};
			var content:XML = FlexXBEngine.instance.serialize(secondIndex);
			ServiceUtils.callService(SAVE_URL, onSave,content);
		}
		
		private static const DELETE_URL:String = "service/second_index/delete/{id}";
		public function remove(id:String, callback:Function):void {
			var onDelete:Function = function(data:Object):void {
				callback();
			};
			var url:String = DELETE_URL.replace("{id}", id);
			ServiceUtils.callService(url, onDelete);
		}
	}
}
class SingletonForcer
{
}